package org.yanh.es.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yanh.es.domain.ExtMain;
import org.yanh.es.domain.ExtStop;
import org.yanh.es.mapper.ExtMainMapper;
import org.yanh.es.service.IExtMainService;
import org.yanh.es.service.IExtStopService;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("dic")
@RequiredArgsConstructor
public class DicController {

    private final IExtMainService extMainService;
    private final IExtStopService extStopService;
    private final ExtMainMapper extMainMapper;

    //获取远程词典列表
    @RequestMapping("test")
    public List<ExtMain> findAllDics() throws IOException {
        ExtMain extMain =extMainMapper.selectById(1);
//        QueryWrapper<ExtMain> qw = new QueryWrapper();
//        qw.between("update_time","2025-04-15","2025-05-16");
        //使用lambda实现
        LambdaQueryWrapper<ExtMain> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(ExtMain::getUpdateTime, "2025-04-15", "2025-05-16");
        return extMainMapper.selectList(lambdaQueryWrapper);
    }


    //获取远程词典列表
    @RequestMapping("findAll")
    public List<String> findAllDics(HttpServletRequest request) throws IOException {
        //定义list
        List<ExtMain> result= Db.lambdaQuery(ExtMain.class).eq(ExtMain::getIsDeleted, 0).list();
        List<String> words=result.stream().map(ExtMain::getWord).collect(Collectors.toList());
        return words;
    }

    //添加热词
    @RequestMapping("save")
    public Map<String, Object> saveDic(String keyword, HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        try{
            QueryWrapper<ExtMain> qw = new QueryWrapper();
            qw.eq("word",keyword);
            var exsit=extMainService.getOne(qw);
            if(exsit==null){
                ExtMain extMain=new ExtMain();
                extMain.setWord(keyword);
                extMainService.save(extMain);
                result.put("success",true);
            }
            else if(exsit!=null&&exsit.getIsDeleted()==1)
            {
                exsit.setIsDeleted(0);
                extMainService.saveOrUpdate(exsit);
                result.put("success",true);
            }else{
                throw new RuntimeException("热词已经存在,无须重复添加!!!");
            }
            result.put("success",true);
        }catch (Exception e){
            e.printStackTrace();
            result.put("success",false);
        }
        return result;
    }

    //删除热词
    @RequestMapping("delete")
    public Map<String, Object> delete(String keyword, HttpServletRequest request) throws IOException {
        Map<String,Object> result = new HashMap<String,Object>();
        try {
            QueryWrapper<ExtMain> qw = new QueryWrapper();
            qw.eq("word",keyword);
            var exsit=extMainService.getOne(qw);
            if( exsit!=null){
                exsit.setIsDeleted(1);
                extMainService.saveOrUpdate(exsit);
                result.put("success",true);
            }else{
                throw new RuntimeException("删除热词失败!!!");
            }
        }catch (Exception e){
            result.put("success",false);
            result.put("message",e.getMessage());
        }
        return result;
    }


    //获取停用词典列表
    @RequestMapping("findStopAll")
    public List<String> findStopAll(HttpServletRequest request) throws IOException {
        //定义list
        List<ExtStop> result= Db.lambdaQuery(ExtStop.class).eq(ExtStop::getIsDeleted, 0).list();
        List<String> words=result.stream().map(ExtStop::getWord).collect(Collectors.toList());
        return words;
    }

    //添加停用词
    @RequestMapping("saveStop")
    public Map<String, Object> saveStop(String keyword, HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        try{
            QueryWrapper<ExtStop> qw = new QueryWrapper();
            qw.eq("word",keyword);
            var exsit=extStopService.getOne(qw);
            if(exsit==null){
                ExtStop extStop=new ExtStop();
                extStop.setWord(keyword);
                extStopService.save(extStop);
                result.put("success",true);
            } else if (exsit!=null&&exsit.getIsDeleted()==1) {
                exsit.setIsDeleted(0);
                extStopService.saveOrUpdate(exsit);
                result.put("success",true);
            } else{
                throw new RuntimeException("停用词已经存在,无须重复添加!!!");
            }
            result.put("success",true);
        }catch (Exception e){
            e.printStackTrace();
            result.put("success",false);
        }
        return result;
    }

    //删除停用词
    @RequestMapping("deleteStop")
    public Map<String, Object> deleteStop(String keyword, HttpServletRequest request) throws IOException {
        Map<String,Object> result = new HashMap<String,Object>();
        try {
            QueryWrapper<ExtStop> qw = new QueryWrapper();
            qw.eq("word",keyword);
            var exsit=extStopService.getOne(qw);
            if(exsit!=null){
                exsit.setIsDeleted(1);
                extStopService.saveOrUpdate(exsit);
                result.put("success",true);
            }else{
                throw new RuntimeException("停用词已经存在,无须重复添加!!!");
            }
        }catch (Exception e){
            result.put("success",false);
            result.put("message",e.getMessage());
        }
        return result;
    }
}
